var express = require('express');
const { uploadFile } = require("tools-factory-tang");
const { updataimgs,updataposter,deleteImg, deletePoster } = require("../api/movies");
// 内置模块 控制文件，fs身上有一个删除的方法unlink，unlink()调用的时候要穿一个绝对路径
const fs = require("fs");
const util = require("util");
const unlink = util.promisify(fs.unlink);
const path = require("path");
var router = express.Router();
// 上传电影剧集
router.post("/uploadImgs", async function (req, res, next) {
  const { data } = await uploadFile(req, { fileType: "moviesimgs" });
  const result = await updataimgs(data);
  res.send(result);
});
// 上传电影海报
router.post("/uploadPoster", async function (req, res, next) {
  const { data } = await uploadFile(req, { fileType: "moviesposter" });
  const result = await updataposter(data);
  res.send(result);
});

// 删除图片数据
router.delete(`/:_id/imgs/:url`, async function (req, res, next) {
  // 从url拿参数
  const { _id, url } = req.params;
  // 获取删除后的数据
  const data = await deleteImg({ _id, url});
  res.send(data)
});

router.delete(`/:_id/imgs/:url`, async function (req, res, next) {
  // 从url拿参数
  const { _id, url } = req.params;
  // 获取删除后的数据
  const data = await deletePoster({ _id, url});
  res.send(data)
});



module.exports = router;
